package com.GLM.Viewer.FirstLogin;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.NavUtils;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;

import com.GLM.Constants.Constant;
import com.GLM.Entity.MsgEntity;
import com.GLM.Tools.Utils.ViewsUtil;
import com.GLM.Tools.Utils.JSON.JSONResponse;
import com.GLM.Tools.Utils.JSON.JSONResponse.Status;
import com.GLM.Tools.Utils.JSON.JSONUtil;
import com.GLM.Tools.Utils.RefreshListView.XListView;
import com.GLM.Tools.Utils.RefreshListView.XListView.IXListViewListener;
import com.GLM.Viewer.R;
import com.google.gson.Gson;

public class LookAroundActivity extends Activity implements IXListViewListener {
	private final static int MSG_FIRSTLOADING = 111;
	private final static int MSG_FIRSTLONDED = 222;
	private XListView mListView;
	private ProgressBar mProgressBar;

	private SimpleAdapter mAdapter;
	private List<Map<String, Object>> mItems = new ArrayList<Map<String, Object>>();
	private int page = 1;

	private final Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_FIRSTLOADING:
				Thread thread = new Thread(new Runnable() {
					public void run() {
						getDataItems();
						mAdapter = new SimpleAdapter(LookAroundActivity.this, mItems, R.layout.xlistview_item_lookaround, new String[] { "date", "nickname", "avatar", "contents" }, new int[] { R.id.list_item_date, R.id.list_item_nickname, R.id.list_item_avatar, R.id.list_item_contents });
						Message m = new Message();
						m.what = MSG_FIRSTLONDED;
						mHandler.sendMessage(m);
					}
				});
				thread.start();
				break;

			case MSG_FIRSTLONDED:
				mListView.setAdapter(mAdapter);
				onLoad();
				mProgressBar.setVisibility(View.GONE);
				break;
			}
		}
	};

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_lookaround);
		mProgressBar = (ProgressBar) findViewById(R.id.progressbar_firstload);

		if (mItems == null || mItems.size() == 0) {
			mProgressBar.setVisibility(View.VISIBLE);
			Message m = new Message();
			m.what = MSG_FIRSTLOADING;
			mHandler.sendMessage(m);
		}
		mListView = (XListView) findViewById(R.id.xListView);
		mListView.setPullLoadEnable(true);
		mListView.setXListViewListener(this);
	}

	private void getDataItems() {
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String currentDate = sDateFormat.format(new java.util.Date());
		JSONObject request = new JSONObject();
		try {
			request.put(Constant.PARAM_LOOKAROUND_DATE, currentDate);
			request.put(Constant.PARAM_LOOKAROUND_PAGE, page);
			JSONResponse jsonResponse = JSONUtil.postRequest(Constant.REQ_WAY_LOOKAROUND, request);
			if (jsonResponse.getStatus() != Status.ERR) {
				JSONObject data = jsonResponse.getData();
				int status = -1;
				status = data.getInt(Constant.RESP_STATUS);
				if (status == Constant.RESP_STATUS_SUCCESS) {
					JSONArray jsonArray = data.getJSONArray(Constant.RESP_ALLMSG);
					Gson gson = new Gson();
					for (int i = 0; i < jsonArray.length(); i++) {
						MsgEntity msgEntity = gson.fromJson(jsonArray.getJSONObject(i).toString(), MsgEntity.class);
						Map<String, Object> item = new HashMap<String, Object>();
						item.put("date", msgEntity.getMsg_date());
						item.put("nickname", msgEntity.getMsg_user_nickname());
						if (msgEntity.getMsg_user_avatar() == null) {
							item.put("avatar", R.drawable.glm_avarat_testpic);
						} else {
						}
						item.put("contents", msgEntity.getMsg_content());
						mItems.add(item);
					}
				} else {
					ViewsUtil.showErrorTip(LookAroundActivity.this, R.string.global_error_networkerr);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

	}

	public void goBackButton_OnClick(View v) {
		NavUtils.navigateUpFromSameTask(this);
	}

	private void onLoad() {
		mListView.stopRefresh();
		mListView.stopLoadMore();
		mListView.setRefreshTime("刚刚");
	}

	public void onRefresh() {
		mHandler.postDelayed(new Runnable() {
			public void run() {
				page = 1;
				mItems.clear();
				getDataItems();
				mAdapter = new SimpleAdapter(LookAroundActivity.this, mItems, R.layout.xlistview_item_lookaround, new String[] { "date", "nickname", "avatar", "contents" }, new int[] { R.id.list_item_date, R.id.list_item_nickname, R.id.list_item_avatar, R.id.list_item_contents });
				mListView.setAdapter(mAdapter);
				onLoad();
			}
		}, 2000);
	}

	public void onLoadMore() {
		page += 1;
		mHandler.postDelayed(new Runnable() {
			public void run() {
				getDataItems();
				mAdapter.notifyDataSetChanged();
				onLoad();
			}
		}, 2000);
	}

	
}